﻿Imports System.Windows.Forms

Public Class PaymentDatesDialog

    WithEvents dtp As New DateTimePicker

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click

        If Trim(DescriptionBox.Text) = "" Then
            DescriptionBox.Focus()
            MessageBox.Show("You must enter a description", "No Description", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        If DatesList.Items.Count = 0 Then
            MessageBox.Show("You cannot add a payment type with no payment dates", "No payment dates", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub dtp_ValueChanged(ByVal sender, ByVal e) Handles dtp.ValueChanged
        'Me.Text = dtp.Value.ToString
    End Sub

    Private Sub PaymentDatesDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If ToolStrip1.Items.Count < 4 Then
            ToolStrip1.Items.Add(New ToolStripControlHost(dtp))
        End If

        dtp.Value = Now

        DatesList.Sorting = SortOrder.Ascending

    End Sub

    Private Sub AddBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddBut.Click
        'first check if this date already exists in the list

        Dim n As Integer

        If DatesList.Items.Count <> 0 Then

            For n = 0 To DatesList.Items.Count - 1
                If DatesList.Items(n).Text = dtp.Value.Date.ToShortDateString Then
                    MessageBox.Show("This date already exists in the list", "Date exists", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Exit Sub
                End If
            Next

        End If


        DatesList.Items.Add(dtp.Value.Date.ToShortDateString)


    End Sub

   

    Private Sub RemoveBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveBut.Click
        If DatesList.SelectedItems.Count = 0 Then Exit Sub

        DatesList.SelectedItems.Item(0).Remove()

    End Sub
End Class
